package com.mvp.shq.washer.utils;

/**
 * Created by Administrator on 2017/5/5.
 */
import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

    /**
     * 返回unix时间戳 (1970年至今的秒数)
     * @return
     */
    public static long getUnixStamp(){
        return System.currentTimeMillis()/1000;
    }

    /**
     * 得到昨天的日期
     * @return
     */
    public static String getYestoryDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE,-1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String yestoday = sdf.format(calendar.getTime());
        return yestoday;
    }

    /**
     * 得到今天的日期
     * @return
     */
    public static String getTodayDate(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(new Date());
        return date;
    }
    public static String getDate(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str = sdf.format(date);
        return str;
    }

    /**
     * 时间戳转化为时间格式
     * @param timeStamp
     * @return
     */
    public static String timeStampToStr(long timeStamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = sdf.format(timeStamp * 1000);
        return date;
    }

    /**
     * 得到日期   yyyy-MM-dd
     * @param timeStamp  时间戳
     * @return
     */
    public static String formatDate(long timeStamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
        String date = sdf.format(timeStamp*1000);
        return date;
    }

    /**
     * 得到时间  HH:mm:ss
     * @param timeStamp   时间戳
     * @return
     */
    public static String getTime(long timeStamp) {
        Date date = new Date(timeStamp);
        return getTime(date);
//        String time = null;
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String date = sdf.format(timeStamp * 1000);
//        String[] split = date.split("\\s");
//        if ( split.length > 1 ){
//            time = split[1];
//        }
//        return time;
    }
    /**
     * 得到时间  HH:mm:ss
     * @param date   时间戳
     * @return
     */
    public static String getTime(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        String time = "";
        time += calendar.get(Calendar.YEAR)+".";
        time += (calendar.get(Calendar.MONTH )+1)+".";
        time += calendar.get(Calendar.DAY_OF_MONTH);
        return time;
    }
    /**
     * 字符串转换成日期
     * @param str
     * @return date
     */
    public static Date StrToDate(String str) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 将一个时间戳转换成提示性时间字符串，如刚刚，1秒前
     *
     * @param timeStamp
     * @return
     */
    public static String convertTimeToFormat(long timeStamp) {
        long curTime = System.currentTimeMillis() / (long) 1000 ;
        long time = curTime - timeStamp;

        if (time < 60 && time >= 0) {
            return "刚刚";
        } else if (time >= 60 && time < 3600) {
            return time / 60 + "分钟前";
        } else if (time >= 3600 && time < 3600 * 24) {
            return time / 3600 + "小时前";
        } else if (time >= 3600 * 24 && time < 3600 * 24 * 30) {
            return time / 3600 / 24 + "天前";
        } else if (time >= 3600 * 24 * 30 && time < 3600 * 24 * 30 * 12) {
            return time / 3600 / 24 / 30 + "个月前";
        } else if (time >= 3600 * 24 * 30 * 12) {
            return time / 3600 / 24 / 30 / 12 + "年前";
        } else {
            return "刚刚";
        }
    }

    /**
     * 格式化时间
     * @param timeStamp
     * @return
     */
    public static String formatDateTime(long timeStamp) {
        String time = formatDate(timeStamp/1000);

        SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm");
        if(time==null ||"".equals(time)){
            return "";
        }
        Date date = new Date(timeStamp);
        /*try {
            date = format.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }*/

        Calendar current = Calendar.getInstance();

        Calendar today = Calendar.getInstance();    //今天

        today.set(Calendar.YEAR, current.get(Calendar.YEAR));
        today.set(Calendar.MONTH, current.get(Calendar.MONTH));
        today.set(Calendar.DAY_OF_MONTH,current.get(Calendar.DAY_OF_MONTH));
        //  Calendar.HOUR——12小时制的小时数 Calendar.HOUR_OF_DAY——24小时制的小时数
        today.set( Calendar.HOUR_OF_DAY, 0);
        today.set( Calendar.MINUTE, 0);
        today.set(Calendar.SECOND, 0);

        Calendar yesterday = Calendar.getInstance();    //昨天

        yesterday.set(Calendar.YEAR, current.get(Calendar.YEAR));
        yesterday.set(Calendar.MONTH, current.get(Calendar.MONTH));
        yesterday.set(Calendar.DAY_OF_MONTH,current.get(Calendar.DAY_OF_MONTH)-1);
        yesterday.set( Calendar.HOUR_OF_DAY, 0);
        yesterday.set( Calendar.MINUTE, 0);
        yesterday.set(Calendar.SECOND, 0);

        current.setTime(date);

        if(current.after(today)){
            String s = "今天 ";
            if (date.getHours() < 10){
                s += "0";
            }
            s += date.getHours() + ":";
            if (date.getMinutes() < 10){
                s += "0";
            }
            s += date.getMinutes();
            return s;
        }else if(current.before(today) && current.after(yesterday)){
            String s = "昨天 ";
            if (date.getHours() < 10){
                s += "0";
            }
            s += date.getHours() + ":";
            if (date.getMinutes() < 10){
                s += "0";
            }
            s += date.getMinutes();
            return s;
        }else{
            String s = (date.getMonth()+1)+"月"+date.getDate()+"日";
            if (date.getHours() < 10){
                s += "0";
            }
            s += date.getHours() + ":";
            if (date.getMinutes() < 10){
                s += "0";
            }
            s += date.getMinutes();
            return s;
            //return (date.getMonth()+1)+"月"+date.getDay()+"日"+date.getHours()+":"+date.getMinutes();
        }
    }

    /**
     * 将一个时间戳转换成提示性时间字符串，(多少分钟)
     *
     * @param timeStamp
     * @return
     */
    public static String timeStampToFormat(long timeStamp) {
        long curTime = System.currentTimeMillis() / (long) 1000 ;
        long time = curTime - timeStamp;
        return time/60 + "";
    }

    public static String getWasherFinishiTime(int min){
        String str = "";
        Date date = new Date();
        long timeStamp = date.getTime()/1000;
        timeStamp += (min * 60);
//        Calendar calendar = Calendar.getInstance();
//        calendar.setTime(new Date(timeStamp));
//        str += calendar.get(Calendar.HOUR)+":" + calendar.get(Calendar.MINUTE);
        SimpleDateFormat f = new SimpleDateFormat("HH:mm");
        str = f.format(new Date(timeStamp*1000));
        return str;
    }
    public static double getProgressTime(String creatTime){
        long time1 = StrToDate(creatTime).getTime()/1000;
        long time2 = new Date().getTime()/1000;
        return time2 - time1;
    }


    /**
     * 创建订单时间与当前时间之间的相差的分钟
     *
     * @return
     */
    public static String compareTimeToFormat(String fromDate) {
        Log.d("compareTimeToFormat","创建时间:"+fromDate);
        SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//如2016-08-10 20:40
        StringBuffer buffer=new StringBuffer();

//        String toDate = getDate(new Date());

        long from = 0;
        long to = 0;
//        int days=0;
//        int hours=0;
        int minutes=0;
        try {
            from = simpleFormat.parse(fromDate).getTime();
            String now=simpleFormat.format(new Date());
            to = new Date().getTime();
            Log.d("compareTimeToFormat","当前时间:"+now);
//            days = (int) ((to - from)/(1000 * 60 * 60 * 24));
//            if(days!=0){
//                buffer.append(days);
//                buffer.append("天");
//            }
//            hours = (int) ((to - from)/(1000 * 60 * 60));
//            if(hours!=0){
//                buffer.append(hours);
//                buffer.append("小时");
//            }
            minutes = 15-(int) ((to - from)/(1000 * 60));
            Log.d("compareTimeToFormat","时间相差:"+minutes);
            if(minutes>=0){
                buffer.append(minutes);
                buffer.append("分钟");
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return buffer.toString();
    }
    /**
     * 订单确定收货时间与当前时间之间的相差的分钟
     *
     * @return
     */
    public static String compareDateToFormat(String fromDate) {
        Log.d("compareTimeToFormat","创建时间:"+fromDate);
        SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//如2016-08-10 20:40
        StringBuffer buffer=new StringBuffer();


        long from = 0;
        long to = 0;
        int days=0;
        int hours=0;
        int minutes=0;
        try {
            from = simpleFormat.parse(fromDate).getTime();
            String now=simpleFormat.format(new Date());
            to = new Date().getTime();
            Log.d("compareTimeToFormat","当前时间:"+now);
            days = 15-(int) ((to - from)/(1000 * 60 * 60 * 24));
            if(days!=0){
                buffer.append(days);
                buffer.append("天");
            }
            hours = (int) ((to - from)/(1000 * 60 * 60));
            if(hours!=0){
                buffer.append(hours);
                buffer.append("小时");
            }
            minutes = (int) ((to - from)/(1000 * 60));
            Log.d("compareTimeToFormat","时间相差:"+minutes);
            if(minutes>=0){
                buffer.append(minutes);
                buffer.append("分钟");
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return buffer.toString();
    }

}